package org.anyfiles.digest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.anyfiles.utils.DigestUtil;

public class MD5Digest implements IDigest {

	@Override
	public String getDigest(File file) throws IllegalArgumentException, IOException, NoSuchAlgorithmException {
		MessageDigest messageDigest = MessageDigest.getInstance(DigestFactory.DIGEST_MD5);
        FileInputStream in = new FileInputStream(file);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        messageDigest.update(byteBuffer);
        byte[] bytes = messageDigest.digest();
        return DigestUtil.byteToHex(bytes, 0, bytes.length).toUpperCase();
	}

}
